window.onload = function() {
	// alert('window'); 进入界面就加载该函数
	// init();
	// location.reload();
}

$("#loginExit").click(function(){
	$.ajax({
		type : "POST", // 请求方式
		url : "../jobsys/user/exit", // 发送请求地址
		dataType : "json",
		async : false,
		traditional : true,
		data : {
		},
		beforeSend : function() {
			
		},
		// 请求成功后的回调函数有两个参数
		success : function(re) {
			// var json = eval("(" + data + ")");// 将json类型字符串转换为json对象
			var result = eval(re);
			alert(result.msg);
			if(result.success){
				top.window.location = '../jobsys/user';
			}				
		},
		completed : function() {

		},
		error : function(re) {
			// 请求出错处理 
			//alert("服务器错误");
		}
	});
});

$("#navigator ul li a").on("click",function(){
	var text =$(this).text();
	if(text=="校园招聘"){
		$("#typename").html("校园招聘");
		var sv = document.getElementById('type1');
		var sv2 = document.getElementById('type2');
		sv.className="";
		sv2.className="active";
		
	}else{
		$("#typename").html("职位信息");
		var sv = document.getElementById('type1');
		var sv2 = document.getElementById('type2');
		sv.className="active";
		sv2.className="";
	}
	initpage(1);
});

// 搜索
$("#hd-su").click(function(){
	var sv = document.getElementById('hd-kw2').value;
	$("#keys").html(sv);
	initpage(1);
});

// 清空条件
$("#clearCondition").click(function(){
	var scity = document.getElementById("scity");
	scity.style.display = "none";
	scity.innerText = '';	
		
	var sindustry = document.getElementById("sindustry");
	sindustry.style.display = "none";
	sindustry.innerText = '';	
	    
	var stime = document.getElementById("stime");
	stime.style.display = "none";
	stime.innerText = '';		
	    
	var ssource = document.getElementById("ssource");
	ssource.style.display = "none";
	ssource.innerText = '';
	
	$("#keys").html();
});

// 城市点击事件
$("#citys dl dd a").on("click",function(){
	var text =$(this).text();//这里是获取点击某一个<a>标签的text值
	if(text=='不限'){
		var scity = document.getElementById("scity");
		scity.style.display = "none";
		scity.innerText = '';	
	}else{
		var scity = document.getElementById("scity");
		scity.style.display = "block";
		scity.innerText = text;	
	}

	initpage(1);
});

// 热门标签点击事件
$("#specialtips dl dd a").on("click",function(){
	var text =$(this).text();//这里是获取点击某一个<a>标签的text值
	if(text=='不限'){
		var sindustry = document.getElementById("sindustry");
	    sindustry.style.display = "none";
	    sindustry.innerText = '';	
	}else{
		var sindustry = document.getElementById("sindustry");
	    sindustry.style.display = "block";
	    sindustry.innerText = text;	
	}
    
    initpage(1);
});

// 时间点击事件
$("#times dl dd a").on("click",function(){
	var text =$(this).text();//这里是获取点击某一个<a>标签的text值
	if(text=='不限'){
		var stime = document.getElementById("stime");
	    stime.style.display = "none";
	    stime.innerText = '';	
	}else{
		var stime = document.getElementById("stime");
	    stime.style.display = "block";
	    stime.innerText = text;	
	}
    initpage(1);
});

// 来源点击事件
$("#sources dl dd a").on("click",function(){
	var text =$(this).text();//这里是获取点击某一个<a>标签的text值
	if(text=='不限'){
		var ssource = document.getElementById("ssource");
	    ssource.style.display = "none";
	    ssource.innerText = '';	
	}else{
		var ssource = document.getElementById("ssource");
	    ssource.style.display = "block";
	    ssource.innerText = text;
	}
    
    initpage(1);
});

// 跳转页数
$("#paginationnums").on("click",'a',function(){
	var pagenum = $(this).text();
	pagenum = Number(pagenum);
	// 调用查询方法
	initpage(pagenum);
});

// 上一页
$("#beforepage").click(function(){
	var pagenum = $("#curpage").html();
	var page = Number(pagenum);
	page = page - 1;
	
	// 调用查询方法
	initpage(page);
});

// 下一页
$("#nextpage").click(function(){
	var pagenum = $("#curpage").html();
	var page = Number(pagenum);
	page = page + 1;
	
	// 调用查询方法
	initpage(page);
});

// 首页
$("#fp").click(function(){
	var pagenum = 1;
	// 调用查询方法
	initpage(pagenum);
});

// 尾页eppage
$("#eppage").click(function(){
	var pagenum = $("#totalpages").html();
	var page = Number(pagenum);
	
	// 调用查询方法
	initpage(page);
});

var initpage = function(curpage)
{
	var scity = $("#scity").html();		
	var sindustry = $("#sindustry").html(); 
	var stime = $("#stime").html();
	var ssource = $("#ssource").html();
	var queryStr =$("#keys").html(); //document.getElementById('hd-kw').value;
	var typename = $("#typename").html();
	loadpageBox(typename,queryStr,curpage,scity,sindustry,stime,ssource);
	gopage(typename,queryStr,curpage,scity,sindustry,stime,ssource);
}

function gopage(typename,queryStr,curpage,scity,sindustry,stime,ssource) {
	var totalPages = $("#totalpages").html();
	totalPages = Number(totalPages);
	if (curpage <= 0) {
		alert("已经是首页了");
		return;
	}

	if (curpage != 1 && totalPages != '' && curpage > totalPages) {
		alert("已经是尾页了");
		return;
	}

	$.ajax({
				type : "POST", // 请求方式
				url : "./job/getjobs", // 发送请求地址
				// contentType : "application/json; charset=utf-8",
				dataType : "json",
				async : true,
				traditional : true,
				data : {
					typename:typename,
					curpage : curpage,
					queryStr : queryStr,
					scity : scity,
					sindustry:sindustry,
					stime:stime,
					ssource:ssource
				},
				beforeSend : function() {
					
				},
				// 请求成功后的回调函数有两个参数
				success : function(re) {
					// var json = eval("(" + data + ")");// 将json类型字符串转换为json对象
					var result = eval(re);
					
					var jh = "";
					for(var d in result){
						jh+="<div class=\"jobs-item\">";
						jh+="<dl>";
						jh+="<dt>";
						var title = result[d].title;
						if(null!=result[d].salary&&result[d].salary!=''){
							title+="<b style=\"color:#ff6600\">（"+result[d].salary+"）</b>";
						}
						
						jh+="<a href=\""+result[d].titlelink+"\" target=\"_blank\" title=\""+result[d].title+"\" log=\"\">"+title+"</a><span	class=\"job-tags\">"+result[d].company+"</span>";
						jh+="</dt>";
						jh+="<dd class=\"jobs-place\">"+result[d].city+"</dd>";
						
						var date = new Date(result[d].publishtime);
						var str = date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
						jh+="<dd class=\"jobs-time\">"+str+"</dd>";
						jh+="<dd class=\"jobs-source\">";
						jh+="<a href=\""+result[d].sourcelink+"\" target=\"_blank\"	log=\"mod=joblist&amp;index=29&amp;pos=source&amp;act=as&amp;src=\">"+result[d].source+"</a>";
						jh+="</dd>";
						jh+="</dl>";
						jh+="</div>";
					}
					
					$("#feed-list").html(jh);
				},
				completed : function() {

				},
				error : function() {
					// 请求出错处理 
					// alert('服务器错误');
					top.window.location="./user";
				}
			});
}

// 加载分页信息
function loadpageBox(typename,queryStr,curpage,scity,sindustry,stime,ssource) {
	var totalPages = $("#totalpages").html();
	if (curpage <= 0) {
		alert("已经是首页了");
		return;
	}

	if (curpage != 1 && totalPages != '' && curpage > totalPages) {
		alert("已经是尾页了");
		return;
	}

	$.ajax({
				type : "POST", // 请求方式
				url : "./job/pageBox", // 发送请求地址
				// contentType : "application/json; charset=utf-8",
				dataType : "json",
				async : true,
				traditional : true,
				data : {
					typename:typename,
					curpage : curpage,
					queryStr : queryStr,
					scity : scity,
					sindustry:sindustry,
					stime:stime,
					ssource:ssource
				},
				beforeSend : function() {
					
				},
				// 请求成功后的回调函数有两个参数
				success : function(re) {
					// var json = eval("(" + data + ")");// 将json类型字符串转换为json对象
					var data = eval(re);
					
					// 用户信息
					$("#nickName").text(data.userinfo.userID);
					var right = data.userinfo.right;
					var mc = document.getElementById('mc');
					if(right=='3'){
						mc.style.visibility = "visible";
					}else{
						mc.style.visibility = "hidden";
					}
					
					// 页数信息
					$("#curpage").html(data.curpage);
					$("#totalpages").html(data.pages);
					$("#totalnums").html(data.totalnums);
					document.getElementById('hd-kw2').value = data.queryStr;
					$("#typename").html(data.typename);
					
					var pagehtml="";
					for(var d in data.pagelist){
						if(data.pagelist[d]>data.pages){
							pagehtml+="<a  href=\"javascript:void(0)\" >"+data.pagelist[d]+"</a>";
						}else{
							if(data.curpage==data.pagelist[d]){
								pagehtml+="<a href=\"javascript:void(0)\" class=\"active\">"+data.pagelist[d]+"</a>";
							}else{
								pagehtml+="<a href=\"javascript:void(0)\">"+data.pagelist[d]+"</a>";
							}
						}
					}
					
					// 是否显示上一页
					var beforepage = document.getElementById("beforepage");	
					var fp = document.getElementById("fp");
					if(data.pagelist[0]==1){
						beforepage.style.display = "none";
						fp.style.display="none";
					}else{
						beforepage.style.display = "inline-block";
						fp.style.display="inline-block";
					}
					
					$("#paginationnums").html(pagehtml);
				},
				completed : function() {

				},
				error : function() {
					// 请求出错处理 
					// alert('服务器错误');
					top.window.location="./user";
				}
			});
}

$(document).ready(function() {
//	var source = $("input[name='source']:checked").val();
//	$("input[name='source']").eq(source).attr("checked", "checked");
//	var scity = document.getElementById("scity").innerText();		
//	var sindustry = document.getElementById("sindustry").innerText();    
//	var stime = document.getElementById("stime").innerText();	   
//	var ssource = document.getElementById("ssource").innerText();
//	var curpage = $("#curpage").innerText();
//	var queryStr = $("#hd-kw").val();
//	
	var curpage = $("#curpage").html();		
	initpage(curpage);
	mark = false;
});